import 'package:flutter/material.dart';
import '../../services/ScreenAdatper.dart';
import 'package:provider/provider.dart';
import '../../provider/Cart.dart';
class CartNum extends StatefulWidget {
  Map itemData;
  CartNum(this.itemData,{Key key}) : super(key: key);
  _CartNumState createState() => _CartNumState();
}

class _CartNumState extends State<CartNum> {
  Map _item;
  var _cartProvider;
  @override
  void initState() { 
    super.initState();
    this._item = widget.itemData; 
  } 
  @override
  Widget build(BuildContext context) {
    this._cartProvider = Provider.of<Cart>(context);
    return Container(
      width: ScreenAdatper.width(164),
      decoration:
          BoxDecoration(border: Border.all(width: 1, color: Colors.black12)),
      child: Row(
        children: <Widget>[
          _LeftBut(),
          _CenterInput(),
          _RightBut(),
        ],
      ),
    );
  }

  _LeftBut() {
    return InkWell(
      child: Container(
        alignment: Alignment.center,
        width: ScreenAdatper.width(45),
        height: ScreenAdatper.height(45),
        child: Text("-"),
      ),
      onTap: () {
        setState(() {
          if(this._item['count'] > 1){
            setState(() {
              this._item['count']--;
              this._cartProvider.changeCount();
            });
          } 
        });
      },
    );
  }

  _RightBut() {
    return InkWell(
      child: Container(
        alignment: Alignment.center,
        width: ScreenAdatper.width(45),
        height: ScreenAdatper.height(45),
        child: Text("+"),
      ),
      onTap: () {
        setState(() {
          this._item['count']++;
          this._cartProvider.changeCount();
        });
      },
    );
  }

  _CenterInput() {
    return Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(
          border: Border(
              left: BorderSide(width: 1, color: Colors.black12),
              right: BorderSide(width: 1, color: Colors.black12))),
      width: ScreenAdatper.width(70),
      height: ScreenAdatper.height(45),
      child: Text("${_item['count']}"),
    );
  }
}
